<?php  
class ControllerModuleCurrency extends Controller {
	public function index()
    {
        if($this->config->get('config_currency_auto_select')){
            $this->load->model('localisation/localisation');
            $this->currency->set($this->model_localisation_localisation->getCurrencyCode());
        }else{
            if (isset($this->request->post['currency_code'])) {
                $this->currency->set($this->request->post['currency_code']);

                unset($this->session->data['shipping_method']);
                unset($this->session->data['shipping_methods']);

                if (isset($this->request->post['redirect'])) {
                    $this->redirect($this->request->post['redirect']);
                } else {
                    $this->redirect($this->url->link('common/home'));
                }
            }
        }
		
		$this->language->load('module/currency');
		
    	$this->data['text_currency'] = $this->language->get('text_currency');

		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$connection = 'SSL';
		} else {
			$connection = 'NONSSL';
		}
		
		$this->data['action'] = $this->url->link('module/currency', '', $connection);
		$this->data['currency_code'] = $this->currency->getCode();
		$this->load->model('localisation/currency');
        $this->load->model('localisation/language');

        $languages=$this->model_localisation_language->getLanguages();
		$currencies=$this->model_localisation_currency->getCurrencies();

        $results=array();
        foreach($languages as $language){
            foreach($currencies as $currency){
                $code=$currency['code'];
                if($code=='EUR') $code='DEU';
                if($code=='USD') $code='ENG';
                if(strtolower(substr($code, 0, 2))==$language['code']){
                    $results[$language['sort_order']]=$currency;
                }
            }
        }

        $currentCurrency=$this->currency->getCode() ? $this->currency->getCode() : $this->model_localisation_localisation->getCurrencyCode();
        $this->data['currencies']=array();
		foreach ($results as $result) {
			if ($result['status']) {
   				$this->data['currencies'][] = array(
					'title'        => $result['title'],
					'code'         => $result['code'],
					'symbol_left'  => $result['symbol_left'],
					'symbol_right' => $result['symbol_right'],
                    'class'        => $result['code']==$currentCurrency ? 'class="active"' : '',
				);
			}
		}

		if (!isset($this->request->get['route'])) {
			$this->data['redirect'] = $this->url->link('common/home');
		} else {
			$data = $this->request->get;
			unset($data['_route_']);
			$route = $data['route'];
			unset($data['route']);
			$url = '';
			
			if ($data) $url='&'.urldecode(http_build_query($data, '', '&'));
			$this->data['redirect'] = $this->url->link($route, $url, $connection);
		}	

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/currency.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/currency.tpl';
		} else {
			$this->template = 'default/template/module/currency.tpl';
		}

        if(!$this->config->get('config_currency_auto_select')) $this->render();
	}
}
?>